//
//  GXShopDetailViewController.m
//  YIChuSeller
//
//  Created by 田小椿 on 2018/12/5.
//  Copyright © 2018年 xiangshengjun. All rights reserved.
//

#import "GXShopDetailViewController.h"
#import "GXShopDetailHeaderView.h"
#import "GXShopIntroduceViewCell.h"
#import "PGIndexBannerSubiew.h"
#import "NewPagedFlowView.h"
#import "GXShopBrowSectionView.h"
@interface GXShopDetailViewController ()<UITableViewDelegate,UITableViewDataSource,NewPagedFlowViewDelegate,
NewPagedFlowViewDataSource>
@property (nonatomic,strong)UITableView * detailTableView;
@property (nonatomic,strong)GXShopDetailHeaderView * headerView;
@property (nonatomic,strong)NSDictionary *contentDic;
@property (nonatomic,strong)NSMutableArray *bannerDataSource;
@property (nonatomic,strong)NSMutableArray *menuDataSource;
@property (nonatomic,strong)NSMutableArray *shopDataSource;
@property (nonatomic,strong)NewPagedFlowView *menuFlowView;
@property (nonatomic,strong)NewPagedFlowView *shopFlowView;
@end
static NSString *const shopIntroduceViewCell=@"GXShopIntroduceViewCell";
static NSString *const menuiden = @"cellMenu";
static NSString *const shopiden = @"cellShop";
@implementation GXShopDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kGlobalbgColor;
    [self.view addSubview:self.detailTableView];
    [self requestData];
}
- (void)requestData
{
    
    [ShopAuthenticationManager selectShopAllMessageWithStoreID:StoreId success:^(id responseObject) {
        if (responseObject) {
            self.contentDic = [NSDictionary dictionaryWithDictionary:responseObject[@"result"]];
                self.headerView.contentDict = self.contentDic;
               self.headerView.height = self.headerView.headerViewHeight;
                [self.detailTableView reloadData];
        }
    } fail:^(id responseObject) {
    }];
    [MenuContentManager selectAllFeatureDataSourceWithStoreId:StoreId success:^(id responseObject) {
        [self handleAllDataSourceFormSort:responseObject];
    } fail:^(id responseObject) {
    }];
}
- (void)handleAllDataSourceFormSort:(id)responseObject
{
    self.bannerDataSource = [NSMutableArray array];
    self.menuDataSource = [NSMutableArray array];
    self.shopDataSource = [NSMutableArray array];
    NSDictionary * resultDic = responseObject[@"result"];
    NSArray *serviceArray = [NSArray arrayWithArray:resultDic[@"serviceS"]];
    if (serviceArray.count) {
        for (int i=0; i<serviceArray.count; i++) {
            NSString *lbtStr = [serviceArray[i] objectForKey:@"lbtSige"];
            NSString *tscStr = [serviceArray[i] objectForKey:@"tscpSige"];
            NSString *sptSigeStr = [serviceArray[i] objectForKey:@"sptcSige"];
            if ([lbtStr isEqualToString:@"1"]) {
                [self.bannerDataSource addObject:serviceArray[i]];
            }
            if ([tscStr isEqualToString:@"1"]) {
                [self.menuDataSource addObject:serviceArray[i]];
            }
            if ([sptSigeStr isEqualToString:@"1"]) {
                [self.shopDataSource addObject:serviceArray[i]];
            }
        }
    }
    self.headerView.bannerItems = [NSArray arrayWithArray:self.bannerDataSource];
    [self.detailTableView reloadData];
    [self.shopFlowView reloadData];
    [self.menuFlowView reloadData];
}
- (UITableView *)detailTableView
{
    if (_detailTableView == nil) {
        _detailTableView =[[UITableView alloc] initWithFrame:CGRectMake(0,-Height_NavBar,kUIScreenWidth,kUIScreenHeight+Height_TabBar+Height_NavBar-Height_StatusBar-20) style:UITableViewStyleGrouped];
        _detailTableView.backgroundColor = kGlobalbgColor;
        _detailTableView.delegate=self;
        _detailTableView.dataSource=self;
        _detailTableView.sectionFooterHeight = 0.01f;
        _detailTableView.showsHorizontalScrollIndicator=NO;
        _detailTableView.showsVerticalScrollIndicator=NO;
        _detailTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_detailTableView  registerNib:[UINib nibWithNibName:@"GXShopIntroduceViewCell" bundle:nil] forCellReuseIdentifier:shopIntroduceViewCell];
    }
    return _detailTableView;
}

- (GXShopDetailHeaderView *)headerView
{
    if (_headerView == nil) {
        _headerView = [[GXShopDetailHeaderView alloc]init];
        self.detailTableView.tableHeaderView = _headerView;
    }
    return _headerView;
}
- (NewPagedFlowView*)menuFlowView{
    if (_menuFlowView==nil) {
        _menuFlowView = [[NewPagedFlowView alloc] initWithFrame:CGRectMake(0, 0, kUIScreenWidth,220)];
        [self setNewPagedFlowViewProperty:_menuFlowView];
    }
    return _menuFlowView;
}

- (NewPagedFlowView*)shopFlowView{
    if (_shopFlowView==nil) {
        _shopFlowView = [[NewPagedFlowView alloc] initWithFrame:CGRectMake(0, 0, kUIScreenWidth,220)];
        [self setNewPagedFlowViewProperty:_shopFlowView];
        
    }
    return _shopFlowView;
}
- (void)setNewPagedFlowViewProperty:(NewPagedFlowView *)flowView
{
    flowView.delegate = self;
    flowView.dataSource = self;
    flowView.minimumPageAlpha = 0.4;
    flowView.leftRightMargin = 30;
    flowView.topBottomMargin = 0;
    flowView.orginPageCount = 3;
    flowView.isOpenAutoScroll = NO;
}
#pragma mark ---UITableViewDataSource UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        GXShopIntroduceViewCell * cell = [tableView dequeueReusableCellWithIdentifier:shopIntroduceViewCell];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.introudceStr =[NSString stringWithFormat:@"%@",self.contentDic[@"introduce"]];
        return cell;
    }
    if (indexPath.section == 1) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:menuiden];
        if (cell==nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:menuiden];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            [cell.contentView addSubview:self.menuFlowView];
            [self.menuFlowView reloadData];
        }
        return cell;
    }
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:shopiden];
    if (cell==nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:shopiden];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell.contentView addSubview:self.shopFlowView];
        [self.shopFlowView reloadData];
    }
        return cell;
   
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath  *)indexPath
{
    return indexPath.section ?220:132;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 1 ||section == 2) {
        return 40;
    }
    return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if  (section == 1 ||section == 2) {
        GXShopBrowSectionView * sectionView = [GXShopBrowSectionView sectionView];
        sectionView.title =  section==1 ? @"特色菜品":@"商家特色";
        return sectionView;
    }
    UIView *headView = [[UIView alloc]init];
    headView.backgroundColor = [UIColor clearColor];
    return headView;
}

#pragma mark --NewPagedFlowView Delegate
- (void)didSelectCell:(UIView *)subView withSubViewIndex:(NSInteger)subIndex {
    MyLog(@"1111");
}

- (void)didScrollToPage:(NSInteger)pageNumber inFlowView:(NewPagedFlowView *)flowView{
    MyLog(@"222");
}

- (CGSize)sizeForPageInFlowView:(NewPagedFlowView *)flowView {
    return CGSizeMake(kUIScreenWidth-60,220);
}

#pragma mark --NewPagedFlowView Datasource
- (NSInteger)numberOfPagesInFlowView:(NewPagedFlowView *)flowView {
    if (flowView == self.menuFlowView) {
        return self.menuDataSource.count ;
    }
    return self.shopDataSource.count;
}

- (PGIndexBannerSubiew *)flowView:(NewPagedFlowView *)flowView cellForPageAtIndex:(NSInteger)index{
    PGIndexBannerSubiew * bannerView =[flowView dequeueReusableCell];
    bannerView = [[PGIndexBannerSubiew alloc] init];
    UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0,10, 60, 25)];
    if (flowView == self.menuFlowView) {
        NSString *nameStr = [NSString stringWithFormat:@"%@",[self.menuDataSource[index] objectForKey:@"name"]];
        NSString * imageStr =[self.menuDataSource[index] objectForKey:@"dishesImage"];
        [self setPGIndexBannerSubiew:bannerView label:label name:nameStr imageStr:imageStr];
    }
    NSString *nameStr = [NSString stringWithFormat:@"%@",[self.shopDataSource[index] objectForKey:@"name"]];
    NSString *imageStr =[self.shopDataSource[index] objectForKey:@"dishesImage"];
    [self setPGIndexBannerSubiew:bannerView label:label name:nameStr imageStr:imageStr];
        return bannerView;
}
- (void)setPGIndexBannerSubiew:(PGIndexBannerSubiew *)bannerView label:(UILabel *)label name:(NSString *)nameStr imageStr:(NSString *)imageStr
{
    bannerView.mainImageView.layer.cornerRadius = 3;
    bannerView.mainImageView.clipsToBounds = YES;
    label.backgroundColor = RGBColor(169, 168, 178);
    UIRectCorner corners = UIRectCornerTopRight | UIRectCornerBottomRight;
    [label setBorderWithCornerRadius:15 borderWidth:2 borderColor:[UIColor clearColor] type:corners];
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor darkGrayColor];
    label.font = [UIFont systemFontOfSize:10];
    [bannerView.mainImageView addSubview:label];
    NSString * urlStr = [NSString stringWithFormat:@"%@/%@",kBaseURLIMAGE,imageStr];
    [bannerView.mainImageView sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:[UIImage  imageNamed:@"home_banner1"]];
    label.text = nameStr;
}

@end
